/**
 * 17/gen/2012 - fabio.melillo@gmail.com
 */
package beta.unisannio.fm.interactive;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.unisannio.remote.FactoryRemote;
import org.unisannio.remote.RemoteBroadcaster;

import beta.unisannio.fm.R;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author fabio
 *
 */
public class ResponseActivity extends Activity {
	
	TextView rispostaField;
	Button reply,cancel;
	String action;
	String domanda;
	Bundle bundle;
	boolean immagine;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Bundle bundle=getIntent().getExtras();
        domanda = bundle.getString(HelpMeActivity.questionKey);
        action = bundle.getString("action");
        immagine=bundle.getBoolean("immagine");
        
        setContentView(R.layout.answer);
        TextView label=(TextView) findViewById(R.id.questionText);
        label.setText(domanda);
        rispostaField=(TextView) findViewById(R.id.answerField);
        reply=(Button) findViewById(R.id.reply);
        cancel=(Button) findViewById(R.id.cancel);
        File filePic;
        Bitmap bm;
        if (immagine){
        	bundle.remove("immagine");
			filePic=new File(this.getExternalFilesDir(null),HelpMeActivity.PIC_FILENAME_RCV);
//			String immagine=bundle.getString("immagine");
//			byte[] buf=Base64.decode(immagine, Base64.DEFAULT);
//			FileOutputStream fos=new FileOutputStream(filePic);
//			bm=BitmapFactory.decodeByteArray(buf, 0, buf.length);
//			bm.compress(Bitmap.CompressFormat.JPEG, 10, fos);
//			fos.flush();
//			fos.close();
			
			bm=BitmapFactory.decodeFile(filePic.getAbsolutePath());
			//insert the image in the layout
	        ImageView dark=(ImageView) findViewById(R.id.imageView);
	        dark.setImageBitmap(bm);
	        dark.setAdjustViewBounds(true);  
	        dark.setScaleType(ScaleType.FIT_XY);
        }  
	        
        
        reply.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View arg0) {
				bundle.remove("immagine");
				String risposta=rispostaField.getText().toString();
				FactoryRemote factory=new FactoryRemote();
				RemoteBroadcaster remote=factory.getRemoteBroadcaster(ResponseActivity.this);
				bundle.putString(HelpMeActivity.responseKey, risposta);
				bundle.putString(HelpMeActivity.questionKey,domanda);
				remote.returnRemoteResult(action, bundle);
				ResponseActivity.this.finish();
			}
		});
        
        cancel.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ResponseActivity.this.finish();
				
			}
		});
        
    };

}
